﻿using System;
using DavidJalbert.TinyCarControllerAdvance;
using EVP;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.VehicleController
{
    public class PlayerVehicleController : MonoBehaviour
    {
        public bool active;
        
        public CarEntity _carEntity;
        // 编辑器下车辆驾驶输入
        public TCCAStandardInput editorCarInput;
        
        [Range(0,1)]
        public float steerFactor = 1f;
        
        public float motorValue;
        public float steerValue;
        public bool handBrake;
        public bool vehicleBack;
        
        public float disableHandBrake = 2f;

        public bool brakeSoundPlaying;
        /// <summary>
        /// 是否翻车
        /// </summary>
        /// <returns></returns>
        public bool IsOverturn()
        {
            if(!active) return false;
            return _carEntity.carController.IsOverturn();  
        }
        //翻车重置
        public void ResetVehicle()
        {
            if(!active) return;
            _carEntity.carController.reset();
        }
        public void SetMotorValue(float v)
        {
            if(!active) return;
            motorValue = v;
            _carEntity.carController.setMotor(v);
            if (v > 0.01)
            {
                vehicleBack = false;
            }
        }

        public void SetSteerValue(float v)
        {
            if(!active) return;

            steerValue = v * steerFactor;
            _carEntity.carController.setSteering(v);
        }

        public void SetHandbrake(float v)
        {
            if(!active) return;

            handBrake = v > 0.5;
            _carEntity.carController.setHandbrake(handBrake);
            if (vehicleBack && !handBrake)
            {
                vehicleBack = false;
                motorValue = 0;
                _carEntity.carController.setMotor(0);
            }
        }

        /// <summary>
        /// The speed of the vehicle in m/s.
        /// </summary>
        public float GetSpeed()
        {
            if (!active) return 0;
            return _carEntity.carController.getSpeed();
        }

        private void Update()
        {
            if (!active)
            {
                if (_carEntity)
                {
                    _carEntity.carController.setMotor(0);
                    _carEntity.carController.setSteering(0);
                }
                return;
            }

            if (handBrake && _carEntity.carController.getForwardVelocity() < disableHandBrake)
            {
                handBrake = false;
                motorValue = -1;
                vehicleBack = true;
            }
            else if(handBrake)
            {
                motorValue = 0;
                _carEntity.carController.setMotor(motorValue);
                _carEntity.carController.setHandbrake(true);
            }

            if (vehicleBack)
            {
                motorValue = -1;
                _carEntity.carController.setMotor(-1);
                _carEntity.carController.setHandbrake(false);
            }

            
#if UNITY_EDITOR
          if (_carEntity.carController.isTCCA)
            {
                if (motorValue == 0 && steerValue == 0 && !handBrake)
                {
                    editorCarInput.enableInput = true;
                }
                else
                {
                    editorCarInput.enableInput = false;
                }
            }
#else
            if (_carEntity.carController.isTCCA)
            {
                editorCarInput.enableInput = false;
            }
#endif
            
            float carSpeed = _carEntity.carController.getForwardVelocity();
            float forwardVelocity = _carEntity.carController.getSpeed() * 2.237f;
            bool isHandbrake = _carEntity.carController.getHandbrake();
            
            // 发动机音效
            float engineSoundValue = forwardVelocity;
            engineSoundValue = Mathf.Abs(engineSoundValue);
            _carEntity.audioComponent.loopAdjustToneSound.SetSoundValue(engineSoundValue);
            
            // 刹车音效
            if (isHandbrake && !brakeSoundPlaying && Mathf.Abs(carSpeed) > 1f)
            {
                _carEntity.audioComponent.loopAdjustToneSound.PlaySound(_carEntity.audioComponent.brakeStartSound);
                brakeSoundPlaying = true;
            }
            else if ((!isHandbrake || Mathf.Abs(carSpeed) < 1f) && brakeSoundPlaying)
            {
                _carEntity.audioComponent.loopAdjustToneSound.StopSound(_carEntity.audioComponent.brakeStartSound);
                _carEntity.audioComponent.loopAdjustToneSound.PlayVoice(_carEntity.audioComponent.brakeEndSound);
                brakeSoundPlaying = false;
            }
           
        }

        public void SetCarEntity(CarEntity entity)
        {
            _carEntity = entity;
            editorCarInput.carController = _carEntity.carController;
            editorCarInput.vehicleController = this;
        }

        public void ClearCarEntity()
        {
            _carEntity = null;
            editorCarInput.carController.setMotor(0);
            editorCarInput.carController.setSteering(0);
            editorCarInput.carController.setHandbrake(false);
            editorCarInput.carController.setBoost(0);
            editorCarInput.carController = null;
        }
    }
}
